package com.yxy.flybird;

import java.io.IOException;

import org.andengine.audio.sound.SoundFactory;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.debug.Debug;

import android.R.integer;

public class Cloud {
	public static final float BITMAP_WIDTH = 1200f;
	public static final float BITMAP_HEIGHT = 72f;

	public static final float CLOUD_WIDTH = 120f;
	public static final float CLOUD_HEIGHT = 72f;

	private Scene mScene;

	// cloud
	private static BuildableBitmapTextureAtlas mCloudBitmapTextureAtlas;
	private static TiledTextureRegion mCloudTextureRegion;

	private AnimatedSprite mSprite;

	private float mCloudYOffset, mCloudXOffset;

	public static void onCreateResources(SimpleBaseGameActivity activity) {
		// bird
		mCloudBitmapTextureAtlas = new BuildableBitmapTextureAtlas(
				activity.getTextureManager(), (int) BITMAP_WIDTH,
				(int) BITMAP_HEIGHT, TextureOptions.NEAREST);
		mCloudTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(mCloudBitmapTextureAtlas, activity,
						"enemy_pig.png", 10, 1);
		try {
			mCloudBitmapTextureAtlas
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 0, 0));
			mCloudBitmapTextureAtlas.load();
		} catch (TextureAtlasBuilderException e) {
			e.printStackTrace();
		}
	}

	public Cloud(float mCloudXOffset, float mCloudYOffset,
			VertexBufferObjectManager mVertexBufferObjectManager, Scene mScene) {

		this.mCloudXOffset = mCloudXOffset;
		this.mCloudYOffset = mCloudYOffset;
		this.mScene = mScene;

		mSprite = new AnimatedSprite(mCloudXOffset, mCloudYOffset, CLOUD_WIDTH,
				CLOUD_HEIGHT, mCloudTextureRegion, mVertexBufferObjectManager);
		mSprite.animate(25);
		mSprite.setZIndex(1);
		mScene.attachChild(mSprite);

	}

	// hover stuff
	private static float WRAPAROUND_POINT = (float) (2 * Math.PI);

	private float mHoverStep = 0;

	public void hover(float speed) {
		mHoverStep += 0.13f;
		if (mHoverStep > WRAPAROUND_POINT)
			mHoverStep = 0;

		float x = mSprite.getX();
		float y = mSprite.getY();
		float newY = y + ((float) (7 * Math.sin(mHoverStep)));
		if (newY > (600 - CLOUD_HEIGHT)) {
			newY = 600 - CLOUD_HEIGHT;
		}
		mSprite.setX(x - speed);
		mSprite.setY(newY);

	}

	public AnimatedSprite getSprite() {
		return mSprite;
	}

	public boolean isOnScreen() {
		if (mSprite.getX() < -CLOUD_WIDTH) {
			return false;
		}
		return true;
	}

	public void destroy() {
		mScene.detachChild(mSprite);
	}
}
